<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="昵称" prop="testKey">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入昵称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-refresh"
          size="mini"
          @click="userRefresh"
        >同步</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userList">

      <el-table-column label="微信openid" align="center" prop="openid" />
      <el-table-column label="是否订阅该公众号" align="center" prop="subscribe" >
        <template slot-scope="{row}">
          <el-tag size="medium" type="success" v-if="row.subscribe==1">是</el-tag>
          <el-tag size="medium" type="warning" v-else-if="row.subscribe==0">否</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="微信昵称" align="center" prop="nickname" />
      <el-table-column label="性别" align="center" prop="sex" >
        <template slot-scope="{row}">
          <el-tag size="medium" type="success" v-if="row.sex==1">男</el-tag>
          <el-tag size="medium" type="warning" v-else-if="row.sex==0">女</el-tag>
          <el-tag size="medium" type="info" v-else>未知</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="用户的语言" align="center" prop="language" />
      <el-table-column label="城市" align="center" prop="city" />
      <el-table-column label="省份" align="center" prop="province" />
      <el-table-column label="国家" align="center" prop="country" />
      <el-table-column prop="avatar" label="头像">
        <template slot-scope="{row}">
          <el-image
            style="width: 50px; height: 50px"
            :src="row.headimgurl"
            fit="fill"
            :preview-src-list="[row.headimgurl]"
          />
        </template>
      </el-table-column>
      <el-table-column label="关注时间" align="center" prop="subscribeTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.subscribeTimeEx, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="关注的渠道来源" align="center" prop="subscribe_scene" />
      <el-table-column label="备注" align="center" prop="remark" />

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listWxPublicUser, syncWxPublicUser} from "@/api/weixin/user/user";

export default {

  data() {
    return {
      //按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 测试单表表格数据
      userList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nickName: undefined,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询测试单表列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};

      listWxPublicUser(this.queryParams).then(response => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.nickName="";
      this.handleQuery();
    },

    /** 同步用户  */
    userRefresh() {
      this.loading = true;
      syncWxPublicUser().then(response => {
        if(response.code==200){
          this.getList();
          this.msgSuccess("同步成功");
          this.loading = false;
        }
      });
    },


  }
};
</script>
